% 这个脚本演示了一个简单的摆锤
% CC-BY 4

clc
clear

dt = 0.01;
n = 2000;
theta = zeros(n,1);
t = zeros(n,1);
g=10;
r=1;

theta(1) = 0;
theta(2) = 0;

figure()
k = 1;

for i=2:n-1
  t(i+1) = t(i)+dt;
  theta(i+1) = 2*theta(i) - theta(i-1) + g/r*cosd(theta(i))*dt*dt;

  if mod(k,10) == 1
    clf
    hold on
    axis equal
    axis([-2 2 -2 2])

    scatter(0,0);
    x = r*cosd(theta(i));
    y = -r*sind(theta(i));
    scatter(x,y);
    line([0 x],[0 y])

    drawnow
    pause(0.01)
  end
  k++;
end

